package com.easylinkin.linkappapi.device.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.common.utils.DateUtil;
import com.easylinkin.linkappapi.device.entity.WaterRecords;
import com.easylinkin.linkappapi.device.entity.vo.WaterRecordsVo;
import com.easylinkin.linkappapi.device.entity.vo.WaterStatisticsVo;
import com.easylinkin.linkappapi.device.service.WaterRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * WaterRecords表控制层
 *
 * @author CodeGenerator
 * @date 2022/06/27
 */
@RestController
@Api(value="用水记录控制器",tags={"用水记录控制器"})
@RequestMapping("appWaterRecords")
public class WaterRecordsController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private WaterRecordsService appWaterRecordsService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<WaterRecordsVo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams().getDeviceCode(), "设备号 不能为空");
        IPage<WaterRecords> record = appWaterRecordsService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appWaterRecordsService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appWaterRecords 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody WaterRecords appWaterRecords) {
        return RestBuilders.successBuilder().success((this.appWaterRecordsService.saveOne(appWaterRecords))).build();
    }

    /**
     * 修改数据
     *
     * @param appWaterRecords 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody WaterRecords appWaterRecords) {
        return RestBuilders.successBuilder().success(this.appWaterRecordsService.updateOne(appWaterRecords)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.appWaterRecordsService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody WaterRecordsVo appWaterRecords, HttpServletRequest request, HttpServletResponse response) {
        Assert.notNull(appWaterRecords.getDeviceCode(), "设备号 不能为空");
        appWaterRecordsService.export(appWaterRecords, request, response);
    }

    /**
     * 查询用水统计
     * @param appWaterRecords 实体对象
     * @return
     */
    @PostMapping("getWaterStatistics")
    @ApiOperation("查询用水统计")
    public RestMessage getWaterStatistics(@RequestBody WaterRecordsVo appWaterRecords) {
//        Assert.notNull(appWaterRecords.getDeviceCode(), "设备号 不能为空");
        return RestBuilders.successBuilder()
            .data(appWaterRecordsService.getWaterStatistics(appWaterRecords)).build();
    }

    /**
     * 用水趋势图
     * @param waterRecordsVo
     * @return
     */
    @PostMapping("countWaterStatistics")
    @ApiOperation("用水趋势图")
    public RestMessage countWaterStatistics(@RequestBody WaterRecordsVo waterRecordsVo){
        Assert.notNull(waterRecordsVo, "waterRecordsVo 不能为空");
        Assert.notNull(waterRecordsVo.getStartTime(), "开始时间 不能为空");
        Assert.notNull(waterRecordsVo.getEndTime(), "结束时间 不能为空");
        Assert.notNull(waterRecordsVo.getGrainSize(), "粒度 不能为空");
//        Assert.notNull(waterRecordsVo.getDeviceCode(), "设备号 不能为空");
        DateUtil.checkDate(waterRecordsVo.getGrainSize(), waterRecordsVo.getStartTime(),
            waterRecordsVo.getEndTime());
        List<WaterStatisticsVo> record = appWaterRecordsService.getWaterStatisticsList(waterRecordsVo);
        return RestBuilders.successBuilder(record).build();
    }


}

